package com.outdd.vaietool.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.outdd.vaietool.common.BaseResponse;
import com.outdd.vaietool.common.annotation.AopLog;
import com.outdd.vaietool.common.constants.Constant;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

/**
 * 拦截工具类
 *
 * @author vaie
 */
public class AopUtil {


    /**
     * 获取参数字符串
     *
     * @param pjp
     * @return
     */
    public static String getArgsString(ProceedingJoinPoint pjp) {
        Object[] args = pjp.getArgs();
        String res = "";
        for (Object arg : args) {
            BaseUtil.setTargetNull(arg);
            try {
                if (res.length() > Constant.LONG_ARGS_LENGTH_MAX) {
                    break;
                }
                res += JSONObject.toJSONString(arg);
            } catch (Exception e) {
            }

        }
        return res;
    }

    /**
     * 获取方法注解
     *
     * @param pjp
     * @return
     */
    public static String getAnnotationValue(ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();

        AopLog aopLog = signature.getMethod().getAnnotation(AopLog.class);
        if (ObjectUtils.isNotEmpty(aopLog) && ObjectUtils.isNotEmpty(aopLog.value())) {
            return aopLog.value();
        }


        return aopLog.value();
    }


    /**
     * 获取方法注解
     *
     * @param pjp
     * @param clazz
     * @param property
     * @return
     */
    public static String getAnnotationValue(ProceedingJoinPoint pjp, Class<? extends
            Annotation> clazz, String property) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();

        Annotation annotation = signature.getMethod().getAnnotation(clazz);
        if (ObjectUtils.isNotEmpty(annotation)) {
            Object annotationValue = JavaReflectUtil.getAnnotationValue(annotation, property);
            if (ObjectUtils.isNotEmpty(annotationValue)) {
                return annotationValue.toString();
            }
            return "";
        }

        return null;
    }

    /**
     * 获取方法注解
     *
     * @param obj
     * @param clazz
     * @param property
     * @return
     */
    public static String getAnnotationValue(Object obj, Class<? extends
            Annotation> clazz, String property) {

        if (ObjectUtils.isEmpty(obj)|| ObjectUtils.isEmpty(clazz)|| ObjectUtils.isEmpty(property)) {
            return "";
        }
        Annotation annotation = obj.getClass().getAnnotation(clazz);

        if (ObjectUtils.isNotEmpty(annotation)) {
            Object annotationValue = JavaReflectUtil.getAnnotationValue(annotation, property);
            if (ObjectUtils.isNotEmpty(annotationValue)) {
                return annotationValue.toString();
            }
            return "";
        }

        return null;
    }


    /**
     * 获取结果字符串
     *
     * @param object
     * @return
     */
    public static String getResStr(Object object) {
        if (ObjectUtils.isEmpty(object)) {
            return null;
        }
        String res = JSONObject.toJSONString(object);

        if (res.length() > Constant.LONG_ARGS_LENGTH_MAX) {
            res = res.substring(0, Integer.valueOf(Constant.LONG_ARGS_LENGTH_MAX.toString())) + "...";
        }
        return res;
    }

    /**
     * 获取url及请求类型
     *
     * @return
     */
    public static String urlMethod() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String method = request.getMethod();
        String url = request.getRequestURI();

        if (StringUtils.isNotEmpty(url)) {
            url = "请求地址：" + url;
        }
        if (StringUtils.isNotEmpty(method)) {
            url += " [" + method + "]";
        }

        return url;
    }




    /**
     * 过滤字段
     *
     * @param result
     */
    public static BaseResponse filtrationField(Object result) {
        /**
         * 过滤不需要的字段
         */
        SimplePropertyPreFilter propertyPreFilter = new SimplePropertyPreFilter();
        List<String> excludeFields = new ArrayList<>();
        //要过滤的字段名称 ****
        excludeFields.add("pageSize");
        excludeFields.add("deleted");
        excludeFields.add("limit");
        excludeFields.add("offset");
        excludeFields.add("order");
        excludeFields.add("pageNumber");
        excludeFields.add("sort");
        excludeFields.add("vagueQuery");
        propertyPreFilter.getExcludes().addAll(excludeFields);
        SerializerFeature[] serializerFeatures;
        serializerFeatures = new SerializerFeature[]{
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullListAsEmpty
        };
        String json = JSON.toJSONString(result, propertyPreFilter, serializerFeatures);

        return JSONObject.parseObject(json, BaseResponse.class);
    }
}
